package com.itheima.marking_system.utils.zip.screen;

import com.itheima.marking_system.pojo.common.R;
import com.itheima.marking_system.pojo.common.request.ScreenRequest;
import com.itheima.marking_system.pojo.entity.ElementEntity;
import com.itheima.marking_system.pojo.entity.PictureEntity;
import com.itheima.marking_system.pojo.entity.RegionEntity;
import com.itheima.marking_system.service.ElementService;
import com.itheima.marking_system.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;

public class ScreenUtils {

        public static ElementEntity captureScreen(byte[] bytes , String fileName, RegionEntity region) {
        // 生成一个随机的UUID字符串，用于命名截图文件
            ElementEntity elementEntity = new ElementEntity();
        String uuidString = UUID.randomUUID().toString();
        try {
            // 读取源图片文件
            BufferedImage sourceImage = ImageIO.read(new ByteArrayInputStream(bytes));

            String suffix = fileName.substring(fileName.lastIndexOf("."));
            // 根据传入的区域参数截取图片
            BufferedImage croppedImage = sourceImage.getSubimage(region.getX(), region.getY(), region.getWidth(), region.getHeight());
            // 创建目标文件对象
            ByteArrayOutputStream ele = new ByteArrayOutputStream();
            ImageIO.write(croppedImage, suffix, ele);
            byte[] imageBytes = ele.toByteArray();
            // 保存截图文件

            elementEntity.setData(imageBytes);
            elementEntity.setName(uuidString + suffix);


        } catch (IOException e) {
            // 捕获并打印异常信息
            e.printStackTrace();
        }
        return elementEntity;
    }
}